package com.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Admin;
import com.pojo.AppAdmin;
import com.pojo.Dictionary;
import com.pojo.Shoufei;
import com.service.AdminService;
import com.service.AppAdminService;
import com.service.DictionaryService;
import com.service.ShoufeiService;
import com.util.StringUtil;
import com.util.ValidateHelper;
import com.vo.DictionaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收费Action
 */
@RestController
@RequestMapping("/shoufei")
public class ShoufeiAction {
    @Autowired
    private ShoufeiService shoufeiService;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private AppAdminService appAdminService;
    @Autowired
    private AdminService adminService;
    /**
     * 获取充值记录
     */
    @RequestMapping("/getRechargeRecord")
    public ResponseBean getRechargeRecord(String username,String sfr, int page, int limit, @RequestParam(required = false) String start_time, @RequestParam(required = false) String end_time) {
        if(StringUtil.isNull(start_time)){
            start_time=null;
        }
        if(StringUtil.isNull(end_time)){
            end_time=null;
        }
        if(StringUtil.isNull(username)){
            username=null;
        }
        if(StringUtil.isNull(sfr)){
            sfr=null;
        }
        IPage iPage = shoufeiService.getRechargeRecord(page,limit,username,sfr,start_time,end_time);
        return new ResponseBean(0, iPage.getTotal(), "成功", iPage.getRecords());
    }

    /**
     * 获取收费标准
     * @return
     */
    @RequestMapping("getShoufeiBiaozhun")
    public ResponseBean getShoufeiBiaozhun (BigDecimal money) {
        Dictionary dictionary = dictionaryService.getDicByCode("shijian_shoufei", "shijian_shoufei");
        if (null == dictionary) {
            return ResponseBean.error("未配置收费标准");
        }
        if (null == money) {
            return new ResponseBean(200, "成功", 0);
        }
        Long result = money.multiply(new BigDecimal(dictionary.getValue())).longValue();
        return new ResponseBean(200, "成功", result);
    }

    /**
     * 添加充值记录
     * @return
     */
    @RequestMapping("addRecharge")
    public ResponseBean addRecharge (@RequestBody Shoufei shoufei, HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (null == admin) {
            return ResponseBean.error("登录信息已过期，请重新登录");
        }
        if (null == shoufei.getMoney()) {
            return ResponseBean.error("请输入充值金额");
        }
        if (shoufei.getRecharge_time() <= 0) {
            return ResponseBean.error("充值时长必须大于0");
        }
        shoufei.setCreate_time(new Date());
        shoufei.setSfr(admin.getId());
        if (shoufeiService.save(shoufei)) {
            AppAdmin appAdmin = appAdminService.getById(shoufei.getUid());
            if (null == appAdmin) {
                return ResponseBean.error("用户不存在");
            }
            Integer total = appAdmin.getShenyu_time() + shoufei.getRecharge_time().intValue();
            appAdmin.setShenyu_time(total);
            appAdminService.updateById(appAdmin);
        }
        return ResponseBean.ok("充值成功！");
    }
    /**
     * 查询时间收费配置
     * @return
     */
    @RequestMapping("select_ShijianShoufei_peizhi")
    public ResponseBean select_ShijianShoufei_peizhi () {

        Dictionary dictionary = dictionaryService.getOne(new QueryWrapper<Dictionary>().eq("code","shijian_shoufei").eq("type","shijian_shoufei").last("limit 1"));
        Dictionary dictionary1 = dictionaryService.getOne(new QueryWrapper<Dictionary>().eq("code","zhuce_shijian").eq("type","zhuce_shijian").last("limit 1"));
        if(dictionary != null || dictionary1 != null){
            Map<String,Object> map = new HashMap<>();
            map.put("shijianshoufei",dictionary);
            map.put("zhuceshijian",dictionary1);
            return new ResponseBean(0,1,map);
        }
        return new ResponseBean(1,1,"");
    }
    /**
     * 修改时间收费配置
     * @param dictionary  时间收费配置
     * @return
     */
    @RequestMapping("/addShijianShoufei")
    public ResponseBean addShijianShoufei(@RequestBody DictionaryVo dictionary) {

        Dictionary shijianshoufei = new Dictionary();
        Dictionary zhuceshijian = new Dictionary();
        if(StringUtil.isNull(dictionary.getId())){  //新增时间收费配置
            shijianshoufei.setCode("shijian_shoufei");
            shijianshoufei.setType("shijian_shoufei");
            shijianshoufei.setValue(dictionary.getValue());
            shijianshoufei.setIs_show(1);
            dictionaryService.save(shijianshoufei);
        }
        if(StringUtil.isNull(dictionary.getZcid())){  //新增注册赠送时间配置
            zhuceshijian.setCode("zhuce_shijian");
            zhuceshijian.setType("zhuce_shijian");
            zhuceshijian.setValue(dictionary.getValue2());
            zhuceshijian.setIs_show(1);
            dictionaryService.save(zhuceshijian);
        }
        if(StringUtil.isNotNull(dictionary.getId())){   //修改时间收费配置
            shijianshoufei.setId(dictionary.getId());
            shijianshoufei.setValue(dictionary.getValue());
            dictionaryService.updateById(shijianshoufei);
        }
        if(StringUtil.isNotNull(dictionary.getZcid())){  //修改注册赠送时间配置
            zhuceshijian.setId(dictionary.getZcid());
            zhuceshijian.setValue(dictionary.getValue2());
            dictionaryService.updateById(zhuceshijian);
        }
        return new ResponseBean(200, 1, "操作成功");
    }
    /**
     * 查询时间收费统计
     * @param timeStart  开始时间
     * @param timeEnd    结束时间
     * @param sfr        收费人ID
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("sj_sf_group")
    public ResponseBean sj_sf_group(String timeStart , String timeEnd , String sfr
            , @RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue = "10") Integer limit){
        return shoufeiList(timeStart , timeEnd , sfr , page , limit) ;
    }

    /**
     * 时间收费查询
     * @param timeStart  开始时间
     * @param timeEnd    结束时间
     * @param sfr        收费人ID
     * @param page
     * @param limit
     * @return
     */
    public ResponseBean shoufeiList(String timeStart , String timeEnd , String sfr
            , @RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue = "10") Integer limit){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        if(StringUtil.isNull(timeStart)) {
            timeStart = null ;
        }
        if(StringUtil.isNull(timeEnd)) {
            timeEnd = null ;
        }
        if(StringUtil.isNull(sfr)) {
            sfr = null ;
        }
        try {
            if(timeStart != null) {
                sdf.parse(timeStart) ;
            }
            if(timeEnd != null) {
                sdf.parse(timeEnd) ;
            }
        }catch (Exception e) {
            return ResponseBean.error("日期格式不对") ;
        }
        IPage iPage = shoufeiService.shoufeiList(timeStart , timeEnd , sfr , page , limit) ;
        return new ResponseBean(0 , iPage.getTotal() , "成功" , iPage.getRecords()) ;
    }
    /**
     * 获取所有管理员
     * @return
     */
    @RequestMapping("getcaozuorenall")
    public ResponseBean getcaozuorenall(){
        List<Admin> admins = adminService.list();
        if(admins.isEmpty()){
            return new ResponseBean(500 , "没有找到管理员！" , admins) ;
        }
        return new ResponseBean(200 , "成功" , admins) ;
    }
}
